﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using MbUnit.Framework;
using HFK.DxFluentUITest.Dummy;
using HFK.DxFluentUITest.Dummy.DataObjects;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

namespace HFK.DxFluentUITest.HorizontalGridControl
{
  [TestFixture]
  public class HorizontalGridControlDatabindingTestFixture
  {
    [Test]
    public void TestDataBinding()
    {
      HorizontalGridDataBinding hgrid = new HorizontalGridDataBinding();
      GridControl horizontalGrid = hgrid.Get;

      List<SimpleClass> data = new List<SimpleClass>();
      data.Add(new SimpleClass() { IntValue = 1, StringValue = "S1" });
      data.Add(new SimpleClass() { IntValue = 2, StringValue = "S2" });

      Form form = new Form();
      form.Controls.Add(horizontalGrid);

      horizontalGrid.DataSource = data;
      horizontalGrid.Update();

      form.Show();

      Thread.Sleep(2000);

      Assert.AreEqual("1", (horizontalGrid.MainView as GridView).GetRowCellDisplayText(0, (horizontalGrid.MainView as GridView).Columns[0]));
      Assert.AreEqual("S1", (horizontalGrid.MainView as GridView).GetRowCellDisplayText(0, (horizontalGrid.MainView as GridView).Columns[1]));
      Assert.AreEqual("2", (horizontalGrid.MainView as GridView).GetRowCellDisplayText(1, (horizontalGrid.MainView as GridView).Columns[0]));
      Assert.AreEqual("S2", (horizontalGrid.MainView as GridView).GetRowCellDisplayText(1, (horizontalGrid.MainView as GridView).Columns[1]));

      form.Close();
    }

    [Test]
    public void TestMasterDetailDataBinding()
    {
      HorizontalGridMasterDetailDataBinding hgrid = new HorizontalGridMasterDetailDataBinding();
      GridControl horizontalGrid = hgrid.Get;

      List<ComplexClass> data = new List<ComplexClass>();
      data.Add(new ComplexClass() { IntValue = 1, StringValue = "S1", ChildData = new List<SingleHopClass>() { new SingleHopClass() { StringValue = "S11" } } });
      data.Add(new ComplexClass() { IntValue = 2, StringValue = "S2", ChildData = new List<SingleHopClass>() { new SingleHopClass() { StringValue = "S21" } } });

      Form form = new Form();
      form.Controls.Add(horizontalGrid);

      horizontalGrid.DataSource = data;
      horizontalGrid.Update();

      form.Show();

      Thread.Sleep(2000);

      GridView mainView = (horizontalGrid.MainView as GridView);
      Assert.AreEqual("1", mainView.GetRowCellDisplayText(0, mainView.Columns[0]));
      Assert.AreEqual("S1", mainView.GetRowCellDisplayText(0, mainView.Columns[1]));
      Assert.AreEqual("2", mainView.GetRowCellDisplayText(1, mainView.Columns[0]));
      Assert.AreEqual("S2", mainView.GetRowCellDisplayText(1, mainView.Columns[1]));

      (horizontalGrid.MainView as GridView).ExpandMasterRow(0);

      //Thread.Sleep(2000);

      GridView detailView1 = ((horizontalGrid.MainView as GridView).GetDetailView(0, 0) as GridView);
      Assert.AreEqual("S11", detailView1.GetRowCellDisplayText(0, detailView1.Columns[0]));

      (horizontalGrid.MainView as GridView).CollapseMasterRow(0);
      (horizontalGrid.MainView as GridView).ExpandMasterRow(1);

      //Thread.Sleep(2000);

      GridView detailView2 = ((horizontalGrid.MainView as GridView).GetDetailView(1, 0) as GridView);
      Assert.AreEqual("S21", detailView2.GetRowCellDisplayText(0, detailView2.Columns[0]));

      form.Close();
    }
  }
}
